<?php

abstract class DAO{
    private $conn;
    private $id;
    private $query;
    protected $arrayData;

    public function __construct() {
    }
    
    abstract protected function receivingData($post);
    
    public function insert($table, $post){
      if($_SERVER['REQUEST_METHOD']=='POST'){             
             
          $array = $this->receivingData($post);
          
          $this->conn = DB::getConn();
          $this->conn->connection_db(BANCO);

          $response = $this->conn->insertion($table,  $array);
                    
          $this->id = $this->conn->last_insert_id(); 
          
          $this->conn->desconnect_mysql();
          
          return $response;
      
      }
   }
   
    public function update($table, $post, $id) {
      if($_SERVER['REQUEST_METHOD']=='POST'){
          
          $array = $this->receivingData($post);
          
          $this->conn = DB::getConn();
          $this->conn->connection_db(BANCO);
          $response = $this->conn->update($table, $array, "WHERE id=$id"); 
          
          $this->id = $id;

          $this->conn->desconnect_mysql();
          
          return $response;
      }
    }
    
    public function delete($table, $id) {

          $this->conn = DB::getConn();
          $this->conn->connection_db(BANCO);
          $response = $this->conn->delete_mysql($table, "WHERE id=$id"); 

          $this->conn->desconnect_mysql();
          
          return $response;
    }
    
    public function last_insert_id(){       
        return $this->id;
    }

    public function last_update_id(){       
        return $this->id;
    }    
    
    public function query($query) {
          $this->conn = DB::getConn();
          $this->conn->connection_db(BANCO);

          $result = $this->conn->select($query);

          $this->conn->desconnect_mysql();
          
          return (!empty($result))? $result : false;
    }
     
    public function total_records() {
          $this->conn = DB::getConn();
          $this->conn->connection_db(BANCO);

          $result = $this->conn->select($this->query);
          $response = $this->conn->num_rows($result); 

          $this->conn->desconnect_mysql();
          
          return (!empty($this->query))? $response : false;
    }
    
    public function select_unique($query) {
        $this->conn = DB::getConn();
        $this->conn->connection_db(BANCO);
        
       $response = $this->conn->select_field($query);
       
       $this->conn->desconnect_mysql();
       
       return $response;
    }
  
    public function select_multiple($query) {       
       $this->query = $query;
        
       $this->conn = DB::getConn();
       $this->conn->connection_db(BANCO);
        
       $response = $this->conn->select_fields($query);
       
       $this->conn->desconnect_mysql();
       
       return $response;
    }   
}
?>
